﻿using System;
using System.Text;

namespace SilverTwitterSearch
{
    /// <summary>
    /// Provides several options for the search.
    /// </summary>
    public struct TwitterSearchOptions
    {
        #region Properties
        /// <summary>
        /// Gets or sets the page number.
        /// </summary>
        /// <remarks>
        /// The page number (starting at 1) to return, up to a max of roughly 
        /// 1500 results (based on TweetsPerPage * page).
        /// </remarks>
        public int? Page 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets the tweets per page.
        /// </summary>
        /// <remarks>
        /// The number of tweets to return per page, up to a max of 100.
        /// </remarks>
        public int? TweetsPerPage 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets the maximum Tweet-ID.
        /// </summary>
        public long? MaxId 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets the Geocode.
        /// </summary>
        /// <remarks>
        /// Returns tweets by users located within a given radius of the given 
        /// latitude/longitude.
        /// </remarks>
        public Geocode? Geocode 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets the language.
        /// </summary>
        /// <remarks>
        /// Restricts tweets to the given language, given by an ISO 639-1 code.
        /// </remarks>
        public string Language 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets the result type.
        /// </summary>
        /// <remarks>
        /// Specifies what type of search results you would prefer to receive. 
        /// The default is "recent."
        /// </remarks>
        public ResultType ResultType 
        {
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets if the user is shown.
        /// </summary>
        /// <remarks>
        /// When true, prepends ":" to the beginning of the tweet. This is useful 
        /// for readers that do not display Atom's author field.
        /// </remarks>
        public bool ShowUser 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets the Until-Date.
        /// </summary>
        /// <remarks>
        /// Returns tweets generated before the given date.
        /// </remarks>
        public DateTime? Until 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets the minimum Tweet-ID.
        /// </summary>
        /// <remarks>
        /// Returns results with an ID greater than (that is, more recent than) 
        /// the specified ID. There are limits to the number of Tweets which can 
        /// be accessed through the API. If the limit of Tweets has occured since 
        /// the since_id, the since_id will be forced to the oldest ID available.
        /// </remarks>
        public long? MinId
        {
            get; 
            set; 
        }
        #endregion

        #region Parameter-String
        /// <summary>
        /// Returns a URI-parameter-string for the current set of Twitter-search options.
        /// </summary>
        /// <returns>URI-parameter-string</returns>
        public override string ToString()
        {
            StringBuilder parameters = new StringBuilder();

            if (this.Page != null)
            {
                parameters.Append("&page=" + this.Page);
            }

            if (this.TweetsPerPage != null)
            {
                parameters.Append("&rpp=" + this.TweetsPerPage);
            }

            if (this.MaxId != null)
            {
                parameters.Append("&max_id=" + this.MaxId);
            }

            if (this.Geocode != null)
            {
                parameters.Append("&geocode=" + this.Geocode);
            }

            if (!String.IsNullOrEmpty(this.Language))
            {
                parameters.Append("&lang=" + this.Language);
            }

            if (this.ShowUser)
            {
                parameters.Append("&show_user=true");
            }

            if (this.Until != null)
            {
                parameters.Append("&until=" + this.Until.Value.ToString("YYYY-MM-DD"));
            }

            if (this.MinId != null)
            {
                parameters.Append("&since_id=" + this.MinId);
            }

            parameters.Append("&result_type=" + this.ResultType.ToString().ToLower());

            return parameters.ToString();
        }
        #endregion
    }
}